package com.zx.mes.longsecurity.hyl.domain;

import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import java.util.*;

@Entity
@Table(name = "tuser")
@DynamicInsert(true)
@DynamicUpdate(true)
public class Tuser implements java.io.Serializable ,UserDetails{
    @Id
    @Column(name = "ID", nullable = false, length = 36)
	private String id;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "CREATEDATETIME", length = 19)
	private Date createdatetime;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "MODIFYDATETIME", length = 19)
	private Date modifydatetime;

    @Column(name = "USERNAME", unique = true, nullable = false, length = 100)
	private String username;

    @Column(name = "PASSWORD", nullable = false, length = 100)
	private String password;

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "tuser_trole", joinColumns = { @JoinColumn(name = "TUSER_ID", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "TROLE_ID", nullable = false, updatable = false) })
	private Set<Trole> troles = new HashSet<Trole>(0);

	public Tuser() {
	}

    public Tuser(String id, Date createdatetime, Date modifydatetime, String username, String password, Set<Trole> troles) {
        this.id = id;
        this.createdatetime = createdatetime;
        this.modifydatetime = modifydatetime;
        this.username = username;

        this.password = password;
        this.troles = troles;
    }


	public String getId() {
		return this.id;
	}

	public void setId(String id) {
		this.id = id;
	}


	public Date getCreatedatetime() {
		return this.createdatetime;
	}

	public void setCreatedatetime(Date createdatetime) {
		this.createdatetime = createdatetime;
	}


	public Date getModifydatetime() {
		return this.modifydatetime;
	}

	public void setModifydatetime(Date modifydatetime) {
		this.modifydatetime = modifydatetime;
	}


    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Set<Trole> getTroles() {
		return this.troles;
	}

	public void setTroles(Set<Trole> troles) {
		this.troles = troles;
	}

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {

        List<GrantedAuthority> auths=new ArrayList<>();

        List<Trole> roles=new ArrayList<>(this.troles);
        List<Trole> troles= roles;
        for (Trole trole:troles){
            auths.add(new SimpleGrantedAuthority(trole.getName()));
        }
        return auths;
    }


    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
